﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server.Dao;
using System.Reflection;

namespace MaxStudio.Concrete.WebSite.Controllers
{
    public class FunctionItemsController : Controller
    {
        //
        // GET: /FunctionItems/


        public ActionResult Functiones()
        {
            var model = GetAllofFunctionItems();
            ViewData["FunctionItems"] = model;
            return View();
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Functiones(Models.FunctionesModels models)
        {
            FunctionGroupService serviceObj = ServiceFactory.getFunctionGroupService();

            if (string.IsNullOrEmpty(models.GroupName))
            {
                ModelState.AddModelError("GroupName", "电影名称不能为空!");
            }
            tb_FunctionGroup instance = convertFromModels(models);

            instance.CreateBy = 1;
            instance.CreateDate = DateTime.Now;
            instance.ModifyBy = 1;
            instance.ModifyDate = DateTime.Now;
            OperationResult<tb_FunctionGroup> result = serviceObj.craeteFunctionGroup(instance);
            if (result.operationSuccess)
            {
               // return View(models);
                return RedirectToAction("Functiones"); 
            }
            else
            {
                return null;
            }
             
             
        }
        #region Function Item Method

        public List<tb_FunctionItem> GetAllofFunctionItems()
        {
            FunctionesService serviceObj = ServiceFactory.getFunctionesService();

            OperationResult<List<tb_FunctionItem>> result = serviceObj.getAllFunctionList();

            if (result.operationSuccess)
            {
                return result.obj;
            }
            else
            {
                return null;
            }
        } 

 
        #endregion

        #region Convert Method

        private tb_FunctionGroup convertFromModels(Models.FunctionesModels models)
        {
            tb_FunctionGroup instance = new tb_FunctionGroup();
            instance.GroupName = models.GroupName;
            instance.Description = models.Description;
            instance.IsDelete = models.IsDelete;
            instance.ModifyBy = models.ModifyBy;
            instance.ModifyDate = models.ModifyDate;
            instance.CreateBy = models.CreateBy;
            instance.CreateDate = models.CreateDate;

           return instance;
        }

        #endregion
        public ActionResult TEST()
        {
            return View();
        }
        public ActionResult FlexigridList()
        {
            int page = int.Parse(Request.Form["page"]);
            int rp = int.Parse(Request.Form["rp"]);
            string qtype = Request.Form["qtype"].ToString();
            string query = Request.Form["query"].ToString();
            string sortname = Request.Form["sortname"].ToString();
            string sortorder = Request.Form["sortorder"].ToString();

            FunctionesService serviceObj = ServiceFactory.getFunctionesService();

            OperationResult<List<tb_FunctionItem>> result = serviceObj.getAllFunctionList();

            List<tb_FunctionItem> functionItemList = result.obj.ToList();
            FlexigridObject flexigridObject = new FlexigridObject();
            flexigridObject.page = page;
            flexigridObject.total = result.obj.Count();
            foreach (tb_FunctionItem functionItem in functionItemList)
            {
                FlexigridRow cell = new FlexigridRow()
                {
                    id = functionItem.Id.ToString (),
                    cell = GetPropertyList(functionItem)
                };
                flexigridObject.rows.Add(cell);
            }


            return View("FlexigridObject", flexigridObject);
        }

        private List<string> GetPropertyList(object obj)
        {
            List<string> propertyList = new List<string>();

            Type type = obj.GetType();
            PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo property in properties)
            {
                object o = property.GetValue(obj, null);
                propertyList.Add(o == null ? "" : o.ToString());
            }

            return propertyList;
        }

    }
}
